import os
import launch.event_handlers
from launch import LaunchDescription
from launch.substitutions import LaunchConfiguration
from ament_index_python.packages import get_package_share_directory
import launch
from launch.launch_description_sources import PythonLaunchDescriptionSource
from launch.actions import IncludeLaunchDescription

def generate_launch_description():
    launch_file_dir = os.path.join(get_package_share_directory('description_pkg'), 'launch')
    world = os.path.join(
        get_package_share_directory('description_pkg'),
        'worlds',
        'empty.world'
    )

    use_sim_time = LaunchConfiguration('use_sim_time', default='true')
    
    # 通过 IncludeLaunchDescription 包含另外一个 launch 文件
    launch_gazebo = launch.actions.IncludeLaunchDescription(
        PythonLaunchDescriptionSource([get_package_share_directory(
            'gazebo_ros'), '/launch', '/gazebo.launch.py']),
      	# 传递参数
        launch_arguments={'use_sim_time': use_sim_time,'world':world}.items()
    )
    
    # 添加场景中的柱子
    spawn_objects = IncludeLaunchDescription(
        PythonLaunchDescriptionSource(
            os.path.join(launch_file_dir, 'spawn_objects.launch.py')
        )
    )

    ld = LaunchDescription()
    # gazebo 仿真
    ld.add_action(launch_gazebo)
    ld.add_action(launch.actions.TimerAction(period=5.0,actions=[spawn_objects]))

    return ld
